<!--  -->
<template>
  <div class="myadmin-home">
    <top-header></top-header>
    <h1>数据统计</h1>
    <div class="myadmin-home-container">
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">总用户量:</span>
          <span class="num">{{ userTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">注册用户:</span>
          <span class="num1">{{ userCount }}</span>
        </div>
      </div>
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">总订单量:</span>
          <span class="num">{{ orderTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">新增订单:</span>
          <span class="num1">{{ orderCount }}</span>
        </div>
      </div>
      <div class="home-container-box">
        <div class="container-box-left">
          <span class="title">管理员:</span>
          <span class="num">{{ adminTotalCount }}</span>
        </div>
        <div class="container-box-right">
          <span class="title1">新增管理员:</span>
          <span class="num1">{{ adminCount }}</span>
        </div>
      </div>
    </div>
    <div class="myadmin-home-chart">
      <!-- 
            todo:
            1. servernDate 日期的数据 ["2020-10-20",.....,"2020-10-26"]
            2.servernDayData [[1,2,3,4,5,6,7],[1,2,3,4,5,6,7],[1,2,3,4,5,6,7]]
            // servernDayData[0] 新增用户
            // servernDayData[1] 新增订单
            // servernDayData[2] 新增管理员
         -->
      <tendency
        :seven-dates="sevenDates"
        :seven-date-data="sevenDateData"
      ></tendency>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import TopHeader from "@/components/top-header.vue";
import Tendency from "@/components/charts/tendency.vue";
import {
  getTotalUser,
  getTodayUser,
  getTotalOrder,
  getTodayTotal,
  getAdminCount,
  getTodayAdmin,
} from "@/api/api";
import { DateUtils } from "./utils/DateUtils";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    "top-header": TopHeader,
    tendency: Tendency,
  },
  data() {
    //这里存放数据
    return {
      userTotalCount: 0,
      userCount: 0,
      orderTotalCount: 0,
      orderCount: 0,
      adminTotalCount: 0,
      adminCount: 0,
      sevenDates: [], // ["2020-10-20".....,"2020-10-26"]
      sevenDateData: [], // [[1,2,3,4..],[12,3,3,..],[1,2,3...]]
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    initSevenDayData: function() {
      // 构造 ["2020-10-20"...,"2020-10-26"]
      //
      let sevenDates = [];
      for (let i = 6; i >= 0; i--) {
        // new Date().getTime() 今天的时间戳
        let oldDate = new Date(new Date().getTime() - i * 1000 * 60 * 60 * 24); // 6天前 5天前....今天
        let oldDateString = DateUtils.getFormatDate(oldDate);
        sevenDates.push(oldDateString);
      }
      this.sevenDates = sevenDates;
      // sevenDateData:[] // [[1,2,3,4..],[12,3,3,..],[1,2,3...]]
      // sevenDateData[0] 新增用户
      // sevenDateData[1] 新增订单
      // sevenDateData[2] 新增管理员
      // console.log(sevenDates);
      let sevenDateData = [[], [], []];
      let senvenDateApi = [[], [], []];
      for (let i = 0; i < sevenDates.length; i++) {
        senvenDateApi[0].push(getTodayUser(sevenDates[i])); // 新增用户
        senvenDateApi[1].push(getTodayTotal(sevenDates[i])); // 新增订单
        senvenDateApi[2].push(getTodayAdmin(sevenDates[i])); // 新增管理员
      }
      Promise.all([
        ...senvenDateApi[0],
        ...senvenDateApi[1],
        ...senvenDateApi[2],
      ]).then((results) => {
        // console.log(results);
        // results => 21 => seveDateData = [[],[],[]]
        // sevenDateData[0] = results[0....6]
        // sevenDateData[1] = results[7....13]
        // sevenDateData[2] = results[14....20]
        // results => for =>
        // i=>(0....6)/7=>Math.floor(0.1) => 0
        // i=>(7....13)/7=>Math.floor(1.x) => 1
        // i=>(14....23)/7=>Math.floor(2.x) => 2
        // v=>{count:4,status:1}
        results.map((v, i) => {
          sevenDateData[Math.floor(i / 7)].push(v.count);
        });
        console.log(sevenDateData);
        this.sevenDateData = sevenDateData;
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    getTotalUser().then((res) => {
      console.log(res);
    });
    getTodayUser(2020 / 12 / 19).then((res) => {
      console.log(res);
    });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 初始化图表的7天的数据
    this.initSevenDayData();
    /* a.2 使用 Promise.all 执行多个并发请求 */
    let today = DateUtils.getFormatDate(new Date()); // 2020-10-26
    Promise.all([
      getTotalUser(),
      getTodayUser(today),
      getTotalOrder(),
      getTodayTotal(today),
      getAdminCount(),
      getTodayAdmin(today),
    ]).then((results) => {
      console.log(results);
      this.userTotalCount = results[0].count;
      this.userCount = results[1].count;
      this.orderTotalCount = results[2].count;
      this.orderCount = results[3].count;
      this.adminTotalCount = results[4].count;
      this.adminCount = results[5].count;
    });
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
@import url("../style/mixin.less");
.myadmin-home {
  background-color: white;
  height: 100%;
  h1 {
    text-align: center;
    font-weight: 300;
    height: 50px;
    line-height: 50px;
  }
  .myadmin-home-container {
    display: flex;
    .home-container-box {
      height: 80px;
      border-top: 1px solid #ddd;
      border-right: 1px solid #ddd;
      border-bottom: 1px solid #ddd;
      flex: 1;
      display: flex;
      .container-box-left {
        .fc();
        flex: 1;
        .num {
          font-size: 30px;
          font-weight: 200;
          color: #ff9800;
        }

        .title {
        }
      }

      .container-box-right {
        .fc();
        flex: 1;
        .title1 {
        }

        .num1 {
          font-size: 30px;
          font-weight: 200;
          color: #20a0ff;
        }
      }
    }
  }
}
.myadmin-home-chart{
  margin-top: 30px;
}
</style>
